package com.example.carrealtimepublisher.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SugarUtil {
    /**
     * 处理地图展示的数据格式
     * @param list
     * @return
     */

    public static Map getMapData(List list){
        Map<String, Object> map = new HashMap<>();
        map.put("mapData",list);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status",0);
        resMap.put("data",map);
        return  resMap;
    }

    /**
     * 处理横向柱图展示的数据格式
     */
    public static <T> Map  getBarData(List<T> list){
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        //从service中获取地区统计数据
        //存categories的
        Map<String, Object> map= new HashMap<>();
        List categoriesList = new ArrayList();
        //存data的
        List dateList = new ArrayList();
        Map seriesDataMap = new HashMap();

        for (T t : list) {
            Class<?> aClass = t.getClass();
            try {
                Method getName = aClass.getMethod("getName");
                if(getName==null){
                    System.err.println("实体对象:"+t+"没有name属性，或者没有该属性的get方法，请检查实体属性");
                }
                Object invoke = getName.invoke(t);
                categoriesList.add(invoke);
                Method getValue = aClass.getMethod("getValue");
                Object val = getValue.invoke(t);
                dateList.add(val);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        seriesDataMap.put("data",dateList);
        seriesDataMap.put("name","销售额度");
        map.put("categories",categoriesList);
        List objects = new ArrayList<>();
        objects.add(seriesDataMap);
        map.put("series",objects);
        resMap.put("data",map);
        return resMap;
    }


    /**
     * 处理3D饼状图展示的数据格式
     * @param list
     * @return
     * @param <T>
     */
    public static <T> Map  getPie3DData(List<T> list){
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        Map<String, Object> map= new HashMap<>();
        Map seriesDataMap = new HashMap();
        seriesDataMap.put("data",list);
        List objects = new ArrayList<>();
        objects.add(seriesDataMap);
        map.put("series",objects);
        resMap.put("data",map);
        return resMap;
    }



    /**
     * 处理饼状图展示的数据格式
     * @param list
     * @return
     * @param <T>
     */
    public static <T> Map  getPieData(List<T> list){
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        resMap.put("data",list);
        return resMap;
    }



    /**
     * 处理轮播表格数据格式
     * @param list1
     * @return
     * @param <T>
     */
    public static <T> String  getTablePlayData(List<String> list1,List<T> list2) {
        //初始化表头信息
        StringBuilder jsonBuilder = new StringBuilder("{" +
                "\"status\": 0," +
                "\"data\": {" +
                "\"columns\": [\n");
        for (int i = 0; i < list1.size(); i++) {
            jsonBuilder.append("{\"name\": \"" + list1.get(i) + "\",\"id\": \"col" + (i + 1) + "\"}");
            if (i < list1.size() - 1) {
                jsonBuilder.append(",\n");
            }
        }
        jsonBuilder.append("],\"rows\": [");
        //对查询出来的数据进行遍历，将每一条遍历的结果封装为json的一行数据
        for (int i = 0; i < list2.size(); i++) {

            if (i >= 1) {
                jsonBuilder.append(",");
            }
            jsonBuilder.append("{");
            T t = list2.get(i);
            Class<?> aClass = t.getClass();
            Field[] fields = aClass.getDeclaredFields();
            for (int j = 0; j < fields.length; j++) {
                Field field = fields[j];
                try {
                    field.setAccessible(true);
                    Object o = field.get(t);//获取所有的属性值
                    jsonBuilder.append("\"col" + (j + 1) + "\":\""+o+"\"");
                    if(j<fields.length-1){
                        jsonBuilder.append(",");
                    }
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
            jsonBuilder.append("}");
        }
        jsonBuilder.append("]}}");
        return jsonBuilder.toString();
    }



    /**
     * 数字翻码牌
     * @return
     * @param <T>
     */
    public static <T> Map  getFlipNumberData(T val){
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        resMap.put("data",new BigDecimal(val+""));
        return resMap;
    }

}
